package com.xuequ.answerme.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xuequ.answerme.R;
import com.xuequ.answerme.beans.MixQuesAnsBean;

import java.util.List;

/**
 * Created by Shijie on 2015/9/29.
 */
public class PushRecyclerViewAdapter extends RecyclerView.Adapter<PushRecyclerViewAdapter.ViewHolder> {

    private List<MixQuesAnsBean> mDataList;//数据

    private OnItemClickListener mOnItemClickListener;

    private OnRefreshDoneListener mOnRefreshDoneListener;


    /**
     * 构造方法
     * @param mDataList
     */
    public PushRecyclerViewAdapter(List<MixQuesAnsBean> mDataList) {
        this.mDataList = mDataList;
    }

    /**
     * Item点击接口
     */
    public interface OnItemClickListener{
        void onItemTitleClick(View view, int position);// 点击问题时触发
        void onItemContentClick(View view, int position);// 点击答案时触发
    }

    public interface OnRefreshDoneListener{
        void onRefreshDone();
    }

    /**
     * 接口绑定
     * @param mOnItemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener){
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public void setOnRefreshDoneListener(OnRefreshDoneListener onRefreshDoneListener){
        this.mOnRefreshDoneListener = onRefreshDoneListener;
    }

    /**
     * 返回
     * @param viewGroup
     * @param viewType
     * @return
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.mix_recycler_item,viewGroup,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Log.i("Adapter", "position------------> " +position);
        holder.mTitle.setText(mDataList.get(position).getQuestionBean().getQuestion_title());
        Log.i("Adapter", "title------------> " + mDataList.get(position).getQuestionBean().getQuestion_title());
        holder.mContent.setText(mDataList.get(position).getAnswerBean().getAnswer_content());
        Log.i("Adapter", "answer------------> " + mDataList.get(position).getAnswerBean().getAnswer_content());
        Log.i("Adapter", "-----------------------------------------------");

        /**
         * 设置绑定点击事件
         */
        if(mOnItemClickListener != null){
            holder.mTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemTitleClick(holder.itemView, holder.getLayoutPosition());
                }
            });
            holder.mContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemContentClick(holder.itemView, holder.getLayoutPosition());
                }
            });
        }
    }

    /**
     * 返回Item数
     * @return
     */
    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    /**
     * 建立ViewHolder
     */
    public class ViewHolder extends RecyclerView.ViewHolder{

        public TextView mTitle;
        public TextView mContent;

        public ViewHolder(View itemView) {
            super(itemView);
            mTitle = (TextView) itemView.findViewById(R.id.mix_recycler_item_question_title);
            mContent = (TextView) itemView.findViewById(R.id.mix_recycler_item_question_answer);
            //背景框
        }
    }

    public void changeData(List<MixQuesAnsBean> mixQuesAnsBeans){

        mDataList.clear();

        for(int i = 0; i < mixQuesAnsBeans.size(); i++){
            mDataList.add(i, mixQuesAnsBeans.get(i));
        }
        notifyDataSetChanged();
        if(mOnRefreshDoneListener != null){
            mOnRefreshDoneListener.onRefreshDone();
        }
    }

}
